﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="WindowsStartUpHelper.cs" company="TechBits">
//   Copyright (c) TechBits. All rights reserved.
// </copyright>
// <author>
//   Leandro G. Vacirca.
// </author>
// <summary>
//   Defines the WindowsStartUpHelper type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace IntelliShortCut.Application.Helpers
{
    using Microsoft.Win32;

    /// <summary>
    /// Provides the functionality to enable or disable the application to run on windows startup.
    /// </summary>
    public class WindowsStartUpHelper
    {
        #region "Atributos Privados"

        /// <summary>
        /// Unique instance of the StartUpHelper class.
        /// </summary>
        private static readonly WindowsStartUpHelper _instance = new WindowsStartUpHelper();

        /// <summary>
        /// Registry path to windows startup programs.
        /// </summary>
        private const string CURRENT_USER_RUN_ROOT_MENU = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
        
        #endregion

        #region "Constructores"

        /// <summary>
        /// Initializes static members of the <see cref="WindowsStartUpHelper"/> class.
        /// </summary>
        /// <remarks>
        /// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit.
        /// </remarks>
        static WindowsStartUpHelper()
        {
            return;
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="WindowsStartUpHelper"/> class from being created.
        /// </summary>
        private WindowsStartUpHelper()
        {
        }

        #endregion

        #region "Propiedades Publicas"

        /// <summary>
        /// Gets the unique instance of the StartUpHelper class.
        /// </summary>
        public static WindowsStartUpHelper Instance
        {
            get { return _instance; }
        }

        #endregion

        #region "Metodos Publicos"

        /// <summary>
        /// Adds an application to the windows startup.
        /// </summary>
        /// <param name="appName">The app name.</param>
        /// <param name="appPath">The app full path.</param>
        public void AddApplicationToStartup(string appName, string appPath)
        {
            if (_instance.IsApplicationOnStartup(appName))
            {
                return;
            }

            RegistryKey reg = Registry.CurrentUser.OpenSubKey(CURRENT_USER_RUN_ROOT_MENU, true);
            if (reg != null && !string.IsNullOrEmpty(appName) && !string.IsNullOrEmpty(appPath))
            {
                reg.SetValue(appName, appPath);              
            }

            if (reg != null)
            {
                reg.Close();
            }
        }

        /// <summary>
        /// Removes an application from the windows startup.
        /// </summary>
        /// <param name="appName">The app name.</param>
        public void RemoveApplicationFromStartup(string appName)
        {
            if (!_instance.IsApplicationOnStartup(appName))
            {
                return;
            }

            RegistryKey reg = Registry.CurrentUser.OpenSubKey(CURRENT_USER_RUN_ROOT_MENU, true);
            if (reg == null)
            {
                return;
            }

            reg.DeleteValue(appName, false);
            reg.Close();
        }

        /// <summary>
        /// Checks if an application is already on windows startup.
        /// </summary>
        /// <param name="appName">The app name.</param>
        /// <returns>
        /// True is the application is on windows startup, false otherwise.
        /// </returns>
        public bool IsApplicationOnStartup(string appName)
        {
            RegistryKey reg = Registry.CurrentUser.OpenSubKey(CURRENT_USER_RUN_ROOT_MENU);
            if (reg == null)
            {
                return false;
            }

            var result = reg.GetValue(appName) == null ? false : true;
            reg.Close();

            return result;
        }

        #endregion
    }
}